clear
global cdpath = "`c(pwd)'"
capture log close
capture log using "$cdpath/Output/Figure2_analysis.log", replace
set more off

//Machine 
//Chip: Apple M2
//Memory: 16 GB
//MacOS: Sonoma 14.3.1 (23D60)

//STATA version 18.5
version

//required packages
ssc install require // (used version version 1.3.1 19sep2023)
require require
sreturn list 
ssc install xlincom // (used version 1.2.7  25sep2023)
require xlincom
sreturn list 
ssc install ftools //  (used version 2.49.1 08aug2023)
require ftools
sreturn list 
ssc install reghdfe // (used version 6.12.3 08aug2023)
require reghdfe
sreturn list 
ssc install coefplot // (used version 1.8.6  22feb2023)
require coefplot
sreturn list 
ssc install estout // /(used version 3.31  26apr2022)
require estout
sreturn list 

import delimited "$cdpath/Data/analysis_data3.csv", clear 

///variable reformatting for analysis
destring quatill_ge_date  pfr, replace force 
label variable quatill_ge_date "Quarters Until General Election"
egen quatill_ge_date_f2=group(quatill_ge_date) if quatill_ge_date>=0

egen candidate2office2yearstate_g=group(candidate2office2yearstate)

gen cincumbencystatus2=cincumbencystatus
replace cincumbencystatus2 ="Incumbent" if  cincumbencystatus=="Incumbent"
replace cincumbencystatus2 ="Non-Incumbent" if  cincumbencystatus=="Challenger" |cincumbencystatus=="Open"
egen cincumbencystatus_f2=group(cincumbencystatus2)

gen pfr100=pfr*100

xtset candidate2office2yearstate_g

///Results
//Figure 2a
reghdfe pfr100  i.quatill_ge_date_f2   if quatill_ge_date>=0 &quatill_ge_date<=7 ,absorb(candidate2office2yearstate_g) cluster(office2yearstate )

xlincom  (r1=2.quatill_ge_date_f2 ) (r2=3.quatill_ge_date_f2 ) (r3=4.quatill_ge_date_f2 ) (r4=5.quatill_ge_date_f2 ) (r5=6.quatill_ge_date_f2 ) (r6=7.quatill_ge_date_f2 ) (r7=8.quatill_ge_date_f2 )  ,post
coefplot  ,  levels( 90 ) yline(0, lcolor(black) ) msymbol(D) mcolor(white ) mlcolor(black)  msize(large)  ciopts(lwidth(*3) lcolor(gray)) xsc(reverse) vertical xline(0, lcolor(black) lpattern(dot)) ylabel(,labsize(large)) xtitle("Quarters Prior to General Election Quarter",size(large)) ytitle("Estimated Quarterly Effects on" "Event-Fundraising Proportions" ,size(large))  ylabel(0 "0%" 5 "5%" 10 "10%" 15 "15%" 20 "20%")  xlabel( 0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" ,labsize(large)) 

graph export "$cdpath/Results/Figure2a.pdf", replace
graph export "$cdpath/Results/Figure2a.eps", replace

//Figure 2b
reghdfe pfr100  i.quatill_ge_date_f2   if quatill_ge_date>=0 &quatill_ge_date<=7 &cincumbencystatus2=="Incumbent"  ,absorb(candidate2office2yearstate_g) cluster(office2yearstate )

eststo incni: xlincom  (r1=2.quatill_ge_date_f2)  (r2=3.quatill_ge_date_f2) (r3=4.quatill_ge_date_f2 ) (r4=5.quatill_ge_date_f2) (r5=6.quatill_ge_date_f2 ) (r6=7.quatill_ge_date_f2) (r7=8.quatill_ge_date_f2 ),post

reghdfe pfr100  i.quatill_ge_date_f2   if quatill_ge_date>=0 &quatill_ge_date<=7 &cincumbencystatus2=="Non-Incumbent"  ,absorb(candidate2office2yearstate_g) cluster(office2yearstate )

eststo nincni: xlincom  (r1=2.quatill_ge_date_f2)  (r2=3.quatill_ge_date_f2) (r3=4.quatill_ge_date_f2 ) (r4=5.quatill_ge_date_f2) (r5=6.quatill_ge_date_f2 ) (r6=7.quatill_ge_date_f2) (r7=8.quatill_ge_date_f2 ),post

coefplot (incni, msize(large) label("Incumbents") msymbol(O) mcolor(white ) mlcolor(black) ciopts(lwidth(*3) lcolor(gray)) offset(0.09) levels( 90 )) (nincni, levels( 90 ) label("Non-Incumbents") msymbol(T) msize(large)  mcolor(white) mlcolor(black)  ciopts(lwidth(*3) lcolor(gray)) offset(-0.09)) , legend(position(0) bplacement(neast)) yscale(r(0 30)) yline(0, lcolor(black)) ylabel(,labsize(large)) xsc(reverse) vertical xline(0, lcolor(black) lpattern(dot))  xtitle("Quarters Prior to General Election Quarter",size(large)) ytitle("Estimated Quarterly Effects on" "Event-Fundraising Proportions" ,size(large)) ylabel(0 "0%" 5 "5%" 10 "10%" 15 "15%" 20 "20%" 25 "25%") xlabel(0 "0" 1 "1" 2 "2" 3 "3" 4 "4" 5 "5" 6 "6" 7 "7" ,labsize(large)  ) 

graph export "$cdpath/Results/Figure2b.pdf", replace
graph export "$cdpath/Results/Figure2b.eps", replace

est clear

/// Table A10
eststo: reghdfe pfr100  i.quatill_ge_date_f2   if quatill_ge_date>=0 &quatill_ge_date<=7 ,absorb(candidate2office2yearstate_g) cluster(office2yearstate ) 

eststo: reghdfe pfr100  i.quatill_ge_date_f2   if quatill_ge_date>=0 &quatill_ge_date<=7 &cincumbencystatus2=="Incumbent"  ,absorb(candidate2office2yearstate_g) cluster(office2yearstate ) 

eststo: reghdfe pfr100  i.quatill_ge_date_f2   if quatill_ge_date>=0 &quatill_ge_date<=7 &cincumbencystatus2=="Non-Incumbent"  ,absorb(candidate2office2yearstate_g) cluster(office2yearstate ) 

esttab using "$cdpath/Results/TableA10.tex", title("Estimated Quarterly Effects on" "Event-Fundraising Proportions")  mtitles("All Candidates" "Incumbent Candidates" "Non-Incumbent Candidates")  se(%7.2f) label  nogap  compress star(* 0.10 ** 0.05 *** 0.01) replace unstack b(2)
 
est clear 

/// Table A11
eststo: reghdfe pfr100  quatill_ge_date  if quatill_ge_date>=0 &quatill_ge_date<=7 ,absorb(candidate2office2yearstate_g) cluster(office2yearstate )
eststo: reghdfe pfr100  quatill_ge_date   if quatill_ge_date>=0 &quatill_ge_date<=7 &cincumbencystatus2=="Incumbent"  ,absorb(candidate2office2yearstate_g) cluster(office2yearstate )
eststo: reghdfe pfr100  quatill_ge_date   if quatill_ge_date>=0 &quatill_ge_date<=7 &cincumbencystatus2=="Non-Incumbent"  ,absorb(candidate2office2yearstate_g) cluster(office2yearstate )

esttab using "$cdpath/Results/TableA11.tex", title("Estimated  Effect of Quarter Until Election on" "Event-Fundraising Proportions") mtitles("All Candidates" "Incumbent Candidates" "Non-Incumbent Candidates")  se(%7.2f) label nogap  compress star(* 0.10 ** 0.05 *** 0.01) replace unstack b(2) 

est clear 

reghdfe pfr100  i.quatill_ge_date_f2   if quatill_ge_date>=0 &quatill_ge_date<=7 ,absorb(candFEs=candidate2office2yearstate_g) cluster(office2yearstate )
sum candFEs

capture log close
